# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2023 0penBrain.                                         *
# *                                                                         *
# *   This file is part of FreeCAD.                                         *
# *                                                                         *
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
# *   under the terms of the GNU Lesser General Public License as           *
# *   published by the Free Software Foundation, either version 2.1 of the  *
# *   License, or (at your option) any later version.                       *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful, but        *
# *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
# *   Lesser General Public License for more details.                       *
# *                                                                         *
# *   You should have received a copy of the GNU Lesser General Public      *
# *   License along with FreeCAD. If not, see                               *
# *   <https://www.gnu.org/licenses/>.                                      *
# *                                                                         *
# ***************************************************************************

# This is a build and test workflow for CI of FreeCAD.
# This workflow aims at building and testing FreeCAD on a Conda environment on macOS.

name: Build macOS 13 (Intel)
on:
  workflow_call:
    inputs:
      artifactBasename:
        type: string
        required: true
      testOnBuildDir:
        default: false
        type: boolean
        required: false
      allowedToFail:
        default: false
        type: boolean
        required: false
    outputs:
      reportFile:
        value: ${{ jobs.Build.outputs.reportFile }}

jobs:
  Build:
    runs-on: macos-13
    continue-on-error: ${{ inputs.allowedToFail }}
    env:
      CCACHE_DIR: ${{ github.workspace }}/ccache
      CCACHE_CONFIGPATH: ${{ github.workspace }}/ccache/config
      CCACHE_MAXSIZE: 1G
      CCACHE_COMPILERCHECK: "%compiler% -dumpfullversion -dumpversion" # default:mtime
      CCACHE_COMPRESS: true
      CCACHE_COMPRESSLEVEL: 1
      CC: x86_64-apple-darwin13.4.0-clang
      CXX: x86_64-apple-darwin13.4.0-clang++
      builddir: ${{ github.workspace }}/build/release/
      logdir: /tmp/logs/
      reportdir: /tmp/report/
      reportfilename: ${{ inputs.artifactBasename }}-report.md
    defaults:
      run:
        shell: bash -l {0}
    outputs:
      reportFile: ${{ steps.Init.outputs.reportFile }}

    steps:
      - name: Checking out source code
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup Miniconda
        uses: conda-incubator/setup-miniconda@v3
        with:
          activate-environment: .conda/freecad
          environment-file: conda/conda-env.yaml
          channels: conda-forge,defaults
          channel-priority: true
          miniforge-version: latest
      - name: Install FreeCAD dependencies
        run: |
          ./conda/setup-environment.sh
      - name: Set Environment Variables
        run: |
          echo "CC=$CC" >> "$GITHUB_ENV"
          echo "CXX=$CXX" >> "$GITHUB_ENV"
      - name: Make needed directories, files and initializations
        id: Init
        run: |
          mkdir -p ${{ env.CCACHE_DIR }}
          mkdir -p ${{ env.CCACHE_CONFIGPATH }}
          mkdir -p ${{ env.builddir }}
          mkdir -p ${{ env.logdir }}
          mkdir -p ${{ env.reportdir }}
          echo "reportFile=${{ env.reportfilename }}" >> $GITHUB_OUTPUT
      - name: Generate cache key
        id: genCacheKey
        uses: ./.github/workflows/actions/macos/generateCacheKey
        with:
          compiler: ${{ env.CXX }}
      - name: Restore Compiler Cache
        uses: actions/cache@v4
        with:
          save-always: true
          path: ${{ env.CCACHE_DIR }}
          key: FC-${{ steps.genCacheKey.outputs.cacheKey }}-${{ github.ref }}-${{ github.run_id }}
          restore-keys: |
            FC-${{ steps.genCacheKey.outputs.cacheKey }}-${{ github.ref }}-
            FC-${{ steps.genCacheKey.outputs.cacheKey }}-
      - name: Print CCache statistics before build, reset stats and print config
        run: |
          ccache -s
          ccache -z
          ccache -p
      - name: CMake Configure
        run: |
          mamba run --live-stream -p .conda/freecad cmake --preset conda-macos-release -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/.conda/freecad/opt/freecad
      - name: CMake Build
        run: |
          mamba run --live-stream -p .conda/freecad cmake --build build/release
      - name: Print ccache statistics after Build
        run: |
          ccache -s
      - name: FreeCAD CLI tests on build dir
        if: inputs.testOnBuildDir
        timeout-minutes: 10
        uses: ./.github/workflows/actions/runPythonTests
        with:
          testDescription: "CLI tests on build dir"
          testCommand: ${{ env.builddir }}/bin/FreeCADCmd -t 0
          logFile: ${{ env.logdir }}TestCLIBuild.log
          reportFile: ${{env.reportdir}}${{ env.reportfilename }}
      - name: C++ tests
        timeout-minutes: 1
        uses: ./.github/workflows/actions/runCPPTests/runAllTests
        with:
          reportdir: ${{ env.reportdir }}
          builddir: ${{ env.builddir }}
          reportFile: ${{ env.reportdir }}${{ env.reportfilename }}
      - name: CMake Install
        run: |
          mamba run --live-stream -p .conda/freecad cmake --install build/release
      - name: FreeCAD CLI tests on install
        timeout-minutes: 10
        uses: ./.github/workflows/actions/runPythonTests
        with:
          testDescription: "CLI tests on install"
          testCommand: ${{ github.workspace }}/.conda/freecad/opt/freecad/bin/FreeCADCmd -t 0
          logFile: ${{ env.logdir }}TestCLIInstall.log
          reportFile: ${{env.reportdir}}${{ env.reportfilename }}
      - name: Upload logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.artifactBasename }}-Logs
          path: |
            ${{ env.logdir }}
            /var/crash/*FreeCAD*
      - name: Upload report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.reportfilename }}
          path: |
            ${{env.reportdir}}${{ env.reportfilename }}
